﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Maticsoft.DAL;
using System.Reflection;
using System.IO;

namespace BuildingForm.PersonInfo
{
    public partial class Reissue : Form
    {
        public Reissue()
        {
            InitializeComponent();
            Init();
            //bind();
        }

        private void Init()
        {
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.ShowIcon = false;
            //表头加粗，居中
            foreach (DataGridViewColumn col in this.dataGridView1.Columns)
            {
                col.SortMode = DataGridViewColumnSortMode.NotSortable;
            }
            this.dataGridView1.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            this.dataGridView1.ColumnHeadersDefaultCellStyle.Font = new System.Drawing.Font("SimSun", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.dataGridView1.AllowUserToAddRows = false;
            //班组
            Maticsoft.DAL.Banz_Info info = new Banz_Info();
            DataTable dt = info.GetList("").Tables[0];
            List<string> strList = new List<string>();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                strList.Add(dt.Rows[i]["bzmc"].ToString().Trim());
            }
            List<string> list = new List<string>();
            list.Add("全部");
            list.AddRange(strList);
            this.cboTeam.DataSource = list;
            //年，月
            int[] months = new int[12] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };
            this.cboMonth.DataSource = months;
            this.cboMonth.Text = DateTime.Now.Month.ToString();
            this.cboMonth2.DataSource = months.Clone();
            this.cboMonth2.Text = DateTime.Now.Month.ToString();
            int year = DateTime.Now.Year;
            int[] years = new int[12] { year - 11, year - 10, year - 9, year - 8, year - 7, year - 6, year - 5, year - 4, year - 3, year - 2, year - 1, year };
            this.cboYear.DataSource = years;
            this.cboYear.Text = DateTime.Now.Year.ToString();
        }
        private DataTable  bind()
        {
            Maticsoft.DAL.Kqjl_Info info = new Kqjl_Info();
            string sql = string.Empty;
            int year = Convert.ToInt32(this.cboYear.Text.ToString());
            int month = Convert.ToInt32(this.cboMonth.Text.ToString());
            int month2 = Convert.ToInt32(this.cboMonth2.Text.ToString());
            if (month2 < month)
            {
                MessageBox.Show("开始月份不能大于结束月份");
                return null;
            }
            string bT = year + "/" + month + "/" + "1";
            string eT = year + "/" + month2 + "/" + "1";
            DateTime endT = Convert.ToDateTime(eT).AddMonths(1).AddMilliseconds(-1);
            DateTime beginT = Convert.ToDateTime(bT).AddMilliseconds(1);

            sql = "CONVERT(varchar(10),a.kqsj,120)<='" + endT.ToString("yyyy-MM-dd") + "' and CONVERT(varchar(10),a.kqsj,120)>='" + beginT.ToString("yyyy-MM-dd") + "'";
            string team = this.cboTeam.Text.ToString().Trim();
            if (team != "全部")
            {
                sql += " and c.bzmc='" + team + "'";
            }
            //sql += " order by a.sfzh,a.kqsj";
            DataTable dt = info.GetList5(sql).Tables[0];
            this.dataGridView1.AutoGenerateColumns = false;
            this.dataGridView1.DataSource = dt;
            return dt;
        }



        private void button1_Click(object sender, EventArgs e)
        {
            bind();
        }

    
        private void btnExport_Click(object sender, EventArgs e)
        {

            try
            {
                Sys_Info dao = new Sys_Info();
                string name = string.Empty;
                DataTable dtt = dao.GetList("").Tables[0];
                if (dtt.Rows.Count > 0)
                {
                    name = dtt.Rows[0]["gcmc"].ToString();
                }
                DataTable dt = bind();
                if (dt==null||dt.Rows.Count  == 0)
                {
                    MessageBox.Show("没有数据！"); return;
                }
                int month = Convert.ToInt32(this.cboMonth.Text.ToString());
                int month2 = Convert.ToInt32(this.cboMonth2.Text.ToString());
                string oldStr = "xh,xm,xb,bzmc,gz,sfzh,bank";
                string newStr = "编号,姓名,性别,班组,工种,身份证号,银行（工资）卡开户行及卡号";
                //for (var i = month; i <= month2; i++)
                //{
                //    oldStr += ",aa";
                //    newStr += "," + i + "月";
                //}
                oldStr += ",aa,aa,aa,aa,aa,aa";
                newStr += ",应发工资总额,已发工资总额,应补发工资总额,本次补发工资总额,劳务人员确认签字,备注";
                SaveFileDialog saveFileDialog1 = new SaveFileDialog();
                //设置文件类型   
                saveFileDialog1.Filter = " xls files(*.xls)|*.xls";
                //设置默认文件类型显示顺序   
                saveFileDialog1.FilterIndex = 2;
                //保存对话框是否记忆上次打开的目录   
                saveFileDialog1.RestoreDirectory = true;
                saveFileDialog1.FileName = "劳务人员工资补发表" + DateTime.Now.ToString("yyyyMMdd");
                //点了保存按钮进入   
                if (saveFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    string team = this.cboTeam.Text.ToString().Trim();
                    if (team == "全部")
                    {
                        team = "所有人员";
                    }
                    team = team + "," + this.cboYear.Text.ToString() + "," + this.cboMonth.Text.ToString() + "," + this.cboMonth2.Text.ToString() + "," + name;
                    string fileName = saveFileDialog1.FileName;
                    string[] oldCols = oldStr.Split(',');
                    string[] newCols = newStr.Split(',');
                    ExcelHelper.ExportByReissue(dt, "劳务人员工资补发表", fileName, "Sheet1", oldCols, newCols, team);
                    MessageBox.Show("导出文件成功！"); return;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("导出文件故障！"); return;
            }
        }

        private void cboMonth2_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }
    public class ReissueSalaryInfo
    {
        public int xh { get; set; }
        public string xm { get; set; }
        public string sfzh { get; set; }
        public string gh { get; set; }
        public string bz { get; set; }
        public string gz { get; set; }
        public string xb { get; set; }
        public string bank { get; set; }
        public int cqts { get; set; }
        public decimal xtgs { get; set; }
        public string aa { get; set; }
        public string m1 { get; set; }
        public string m2 { get; set; }
        public string m3 { get; set; }
        public string m4 { get; set; }
        public string m5 { get; set; }
        public string m6 { get; set; }
        public string m7 { get; set; }
        public string m8 { get; set; }
        public string m9 { get; set; }
        public string m10 { get; set; }
        public string m11 { get; set; }
        public string m12 { get; set; }
    }
}

